﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using NtpChat.Client.Model;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using System.Windows;

namespace NtpChat.Client.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {

        UserViewModel currentUser;

        private ObservableCollection<ConversationViewModel> conversations;

        private ObservableCollection<UserViewModel> friends;

        private IService voipService;

        private ViewModelBase topVM;

        private RecordUtil device;

        public MainViewModel()
        {
            voipService = new VoIPService();
            conversations = new ObservableCollection<ConversationViewModel>();
            friends = new ObservableCollection<UserViewModel>();
            Messenger.Default.Register<string>(this, HandleLogin);
            Messenger.Default.Register<GenericMessage<ConversationViewModel>>(this, HandleConversation);
            Messenger.Default.Register<GenericMessage<Window>>(this, HandleRecording);
            device = new RecordUtil();
            device.SendBuffer += new RecordUtil.SendBufferHandler(device_SendBuffer);
            topVM = new LoginViewModel();

        }

        void device_SendBuffer(byte[] buffer, int length)
        {
            device.PlayBuffer(buffer);
            //send buffer over network
        }

        public void HandleRecording(GenericMessage<Window> message)
        {
            if (message.Target.GetType() == typeof(MainViewModel))
            {
               //
            }
        }

        public void HandleLogin(string message)
        {
            if (currentUser == null)
            {
                currentUser = new UserViewModel(new User(message, UserStatus.Online));
                topVM = CurrentUser;
                ViewModelLocator.CurrentUserStatic = CurrentUser;
                RaisePropertyChanged("TopVM");

                Friends.Add(new UserViewModel(new User("user1", UserStatus.Busy)));
                Friends.Add(new UserViewModel(new User("user2", UserStatus.Online)));
                Friends.Add(new UserViewModel(new User("user3", UserStatus.Online)));
                Friends.Add(new UserViewModel(new User("user4", UserStatus.Busy)));
            }
        }


        public ViewModelBase TopVM
        {
            get { return topVM; }
        }


        public UserViewModel CurrentUser
        {
            get { return currentUser; }
        }

        public ObservableCollection<ConversationViewModel> Conversations
        {
            get { return conversations; }
        }

        public ObservableCollection<UserViewModel> Friends
        {
            get { return friends; }
        }

        public void StartRecording()
        {
            device.Initialize();
            device.Play();
        }

        public void OpenConversation(UserViewModel buddy)
        {
            if (buddy != null)
            {
                ConversationViewModel conv = new ConversationViewModel(this.voipService, ViewModelLocator.CurrentUserStatic, buddy);
                if (!Conversations.Contains(conv))
                {
                    Conversations.Add(conv);
                    Messenger.Default.Send<GenericMessage<ConversationViewModel>>(new GenericMessage<ConversationViewModel>(this, typeof(MainWindow), conv));
                }
                
            }
        }

        public bool canOpenConversation(UserViewModel buddy)
        {
            return buddy != null;
        }

        public void HandleConversation(GenericMessage<ConversationViewModel> message)
        {
            if (message.Sender.GetType() == typeof(ConversationView))
            {
                this.Conversations.Remove(message.Content);
            }
        }


        private RelayCommand<UserViewModel> openConversationCommand;

        public RelayCommand<UserViewModel> OpenConversationCommand
        {
            get
            {
                if (openConversationCommand == null)
                    openConversationCommand = new RelayCommand<UserViewModel>(OpenConversation, canOpenConversation);
                return openConversationCommand;
            }
        }

        private RelayCommand startRecordingCommand;

        public RelayCommand StartRecordingCommand
        {
            get
            {
                if (startRecordingCommand == null)
                    startRecordingCommand = new RelayCommand(StartRecording);
                return startRecordingCommand;
            }
        }
    }
}